{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Robodyno Battery API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.初始化Battery对象"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Battery(can, id_)`\n",
    "\n",
    "参数：\n",
    "- `can` : can总线接口\n",
    "- `id_` : Battery电池模块ID，默认`0x0f`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 714,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.components import Battery\n",
    "from robodyno.interfaces import CanBus\n",
    "\n",
    "can = CanBus()\n",
    "battery = Battery(can, 0x0f)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.读取电池电压"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_voltage(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "- 电池电压值 ( V ) ，超时返回 `None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 715,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27.41"
      ]
     },
     "execution_count": 715,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_voltage(1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.读取电池电流"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_current(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "- 电池电流值 ( A ) , 正值表示充电, 超时返回 `None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1166,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 1166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_current()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.读取电池电量"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_capacity(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - tuple:\n",
    "    - remaining capacity(Ah) : 剩余容量\n",
    "    - standard capacity(Ah) : 标称容量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34.17, 38.4)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_capacity()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.读取电池充放电次数"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_cycle_count(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 电池充放电循环次数，超时返回`None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_cycle_count()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.读取电池生产日期"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_manufacture_date(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 生产日期(年, 月, 日), 超时返回`None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(23, 3, 24, 0, 0)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_manufacture_date()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.读取电池的均衡状态"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_equalization_status(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 均衡状态, 超时返回`None`\n",
    "    - `0`: 关闭\n",
    "    - `1`: 打开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_equalization_status()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.读取电池保护状态"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_protect_status(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 保护状态, 超时返回`None`\n",
    "    - `0`: 未保护\n",
    "    - `1`: 发生保护\n",
    "      - `bit0`: 单体过压保护\n",
    "      - `bit1`: 单体欠压保护\n",
    "      - `bit2`: 整组过压保护\n",
    "      - `bit3`: 整组欠压保护\n",
    "      - `bit4`: 充电过温保护\n",
    "      - `bit5`: 充电低温保护\n",
    "      - `bit6`: 放电过温保护\n",
    "      - `bit7`: 放电低温保护\n",
    "      - `bit8`: 充电过流保护\n",
    "      - `bit9`: 放电过流保护\n",
    "      - `bit10`: 短路保护\n",
    "      - `bit11`: 前端检测IC错误\n",
    "      - `bit12`: 软件锁定MOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_protect_status()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.读取电池硬件版本"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_protect_status(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 硬件版本, 超时返回`None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.2'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_hardware_version()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.读取电池剩余容量百分比"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_rsoc(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 剩余容量百分比, 超时返回`None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.89"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_rsoc()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11.读取电池FET控制状态"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_fet(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - MOS指示状态, 超时返回`None`\n",
    "    - `0`: 关闭\n",
    "    - `1`: 打开\n",
    "    - `bit0`: 充电\n",
    "    - `bit1`: 放电"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1140,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 1140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_fet()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12.读取电池串数"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_string_count(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 电池串数。如果超时则返回`None`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_string_count()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.读取电池温度"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_temperature(timeout = None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，默认为`None`\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "  - 电池温度。如果超时则返回`None`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(25.3, 27.0)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battery.get_temperature()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14.设置CAN_ID"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`config_can_bus(new_id, heartbeat = 1000, bitrate = 1000000)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `new_id` ：电池模块的新CAN_ID (默认：`0x0f`)\n",
    "- `heartbeat` : 心跳包发送周期 （ ms ）\n",
    "- `bitrate` : CAN总线通讯速率 (修改后需重新上电生效)\n",
    "    - 1000000\n",
    "    - 500000\n",
    "    - 250000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "battery.config_can_bus(0x0f, heartbeat=1000, bitrate=1000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
